package com.rahwi.rahwi_server.Service.impl;

import com.rahwi.rahwi_server.DAO.CommentDAO;
import com.rahwi.rahwi_server.DAO.PostDAO;
import com.rahwi.rahwi_server.DAO.PostLikeDAO;
import com.rahwi.rahwi_server.DAO.UserDAO;
import com.rahwi.rahwi_server.Entity.Comment;
import com.rahwi.rahwi_server.Entity.Post;
import com.rahwi.rahwi_server.Entity.PostLike;
import com.rahwi.rahwi_server.Entity.User;
import com.rahwi.rahwi_server.Service.Interface.IHomePost;
import com.rahwi.rahwi_server.DefineEntity.HomePost;
import com.rahwi.rahwi_server.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


@Service
public class HomePostService implements IHomePost {
    @Autowired
    private PostDAO postDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private CommentDAO commentDAO;

    @Autowired
    private PostLikeDAO postLikeDAO;
    /**
     *
     * @param pageParams
     * @return List<HomePost>
     * 构造PageRequest对象，调用postDao.findAll(PageRequest) ,userDAO.findById（Id）方法，写回自定义返回对象
     */
    @Override
    public List<HomePost> getHomePostByPage(PageParams pageParams) {
        List<HomePost> homePost = new ArrayList<>();
        PageRequest pageable = PageRequest.of(pageParams.getPage(), pageParams.getSize());
        Page<Post> postPage = postDAO.findAll(pageable);
        List<Post> postData = postPage.getContent();
        //写入返回对象
        for (Post item:postData) {
            System.out.println("item:" + item.getTitle());
            HomePost postItem = new HomePost();
            postItem.setId(item.getPid());
            postItem.setPoster_id(item.getUid());
            postItem.setTitle(item.getTitle());
            System.out.println("user:" + item.getUid());
            User user = userDAO.findById(item.getUid()).get();
            //if(user == null) return null;
            postItem.setPoster_logo(user.getPicture());
            postItem.setPoster_name(user.getUserName());
            postItem.setImage(new String[]{item.getPicture()});
            postItem.setText(item.getContent());

//            Specification<PostLike> Joined = (Specification<PostLike>) (root, criteriaQuery, criteriaBuilder)
//                    -> criteriaBuilder.and(criteriaBuilder.equal(root.get("Pid"), item.getPid(), criteriaBuilder.equal(root.get("Uid"), item.getPid()))

            postItem.setPoster_is_joined(false);
            postItem.setCreated_at(item.getDateTime());
            postItem.setTag(item.getTag());
            Specification<Comment> specification = (Specification<Comment>) (root, criteriaQuery, criteriaBuilder)
                    -> criteriaBuilder.and(criteriaBuilder.equal(root.get("Pid"), item.getPid()));
            postItem.setComment_count(commentDAO.findAll(specification).size()); //未写
            homePost.add(postItem);
        }
        return homePost;
    }

    /**
     *
     * @param
     * @return number
     * 返回帖子总数
     * @Date 2021/12/17 16:22
     * @Autor dn
     * @Version 1.0
     */
    @Override
    public int getPostNumber() {
        return postDAO.findAll().size();
    }

    /**
     *
     * @param
     * @return number
     * 返回帖子总数
     * @Date 2021/12/17 16:22
     * @Autor dn
     * @Version 1.0
     */
    @Override
    public int sendPost(int uid, String content, String picture, String tag, String title) {
        Post post = new Post();
        post.setUid(uid);
        if(userDAO.findById(uid).isEmpty())
            return 0;
        post.setUser(userDAO.findById(uid).get());
        post.setContent(content);
        post.setTag(tag);
        post.setTitle(title);
        post.setDateTime(new Date(System.currentTimeMillis()));
        post.setPicture(picture);
        return postDAO.save(post).getPid();
    }

}
